package com.hongdee.crm.question.service.Impl;

import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.question.dao.ITCrmQuestionGroupItemAreaDao;
import com.hongdee.crm.question.entity.TCrmSurveryQuestionGroupItem;
import com.hongdee.crm.question.entity.TCrmSurveryQuestionGroupItemArea;
import com.hongdee.crm.question.service.ITCrmQuestionGroupItemAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Name:
 * @Author: Liu XiaoLong
 * @Date: 2017/10/16 14:16
 * @Remark:
 */
@Service
public class TCrmQuestionGroupItemAreaServiceImpl implements ITCrmQuestionGroupItemAreaService {

    @Autowired
    private ITCrmQuestionGroupItemAreaDao itCrmQuestionGroupItemAreaDao;
    private String tableName = "t_crm_survery_question_group_item_area";
    @Override
    public List page(String uid, Map data) throws Exception {
        String companyId = TokenUtil.getCompanyId(uid);

                String sql = "SELECT a.*,b.area_code,b.area_name,b.status,b.group_code,b.item_code " +
                        "FROM t_crm_survery_question_group_item_area a LEFT JOIN t_crm_survery_item_area b ON a.area_id = b.id " +
                        "WHERE a.company_id=? ";
        List params = new ArrayList();
        params.add(companyId);
        if(!data.get("questionId").toString().isEmpty()){
            sql += "AND a.quesiton_id = ? ";
            params.add(data.get("questionId").toString());
        }
        if(!data.get("groupId").toString().isEmpty()){
            sql += "AND a.group_id = ? ";
            params.add(data.get("groupId").toString());
        }
        if(!data.get("itemId").toString().isEmpty()){
            sql += "AND a.item_id = ? ";
            params.add(data.get("itemId").toString());
        }
        List list =  itCrmQuestionGroupItemAreaDao.list(sql,params.toArray());

        return list;
    }

    /**
     * @Name: 分值修改
     * @Author: Liu XiaoLong
     * @Date: 2017/10/16 17:36
     * @Remark:
     */
    @Override
    public TCrmSurveryQuestionGroupItemArea saveScore(String id, int score,String itemId,String questionId) throws Exception {

        String sql = "update T_CRM_SURVERY_QUESTION_GROUP_ITEM_AREA set score=? where id = ? ";
        List params = new ArrayList();
        params.add(score);
        params.add(id);
        itCrmQuestionGroupItemAreaDao.executeSQL(sql,params.toArray());

        String maxSql = "select max(score) score from T_CRM_SURVERY_QUESTION_GROUP_ITEM_AREA where item_id = ? and quesiton_id = ? ";
        List maxParams = new ArrayList();
        maxParams.add(itemId);
        maxParams.add(questionId);
        Map map = itCrmQuestionGroupItemAreaDao.info(maxSql,maxParams.toArray());


        String itemSql = "update T_CRM_SURVERY_QUESTION_GROUP_ITEM set score=? where item_id = ? and quesiton_id= ? ";
        List itemParams = new ArrayList();
        itemParams.add(map.get("score"));
        itemParams.add(itemId);
        itemParams.add(questionId);
        itCrmQuestionGroupItemAreaDao.executeSQL(itemSql,itemParams.toArray());
        return null;
    }

    @Override
    public TCrmSurveryQuestionGroupItemArea saveOrUpdate(String uid, Map map) throws Exception {
        List list = (List) map.get("dataList");
        for(int i = 0;i<list.size();i++){
            //TCrmSurveryQuestionGroupItemArea t = (TCrmSurveryQuestionGroupItemArea) list.get(i);
            //System.out.println(t+"__");
            Map m = (Map) list.get(i);
            TCrmSurveryQuestionGroupItemArea tCrmSurveryQuestionGroupItemArea = new TCrmSurveryQuestionGroupItemArea();
            tCrmSurveryQuestionGroupItemArea.setItemId(m.get("itemId").toString());
            tCrmSurveryQuestionGroupItemArea.setCompanyId(m.get("companyId").toString());
            tCrmSurveryQuestionGroupItemArea.setAreaId(m.get("areaId").toString());
            tCrmSurveryQuestionGroupItemArea.setGroupId(m.get("groupId").toString());
            tCrmSurveryQuestionGroupItemArea.setId("");
            tCrmSurveryQuestionGroupItemArea.setQuesitonId(m.get("quesitonId").toString());

            itCrmQuestionGroupItemAreaDao.saveOrUpdate(tCrmSurveryQuestionGroupItemArea);
        }
        return null;
    }

    /**
     * @Name: 获取问题项list
     * @Author: Liu XiaoLong
     * @Date: 2017/10/18 12:39
     * @Remark:
     */
    @Override
    public Map getListByQuestionId(String uid, String questionId) throws Exception {
        String companyId = TokenUtil.getCompanyId(uid);

        String sql = "select * from "+tableName+" where company_id = ? and quesiton_id = ? ";
        List params = new ArrayList();
        params.add(companyId);
        params.add(questionId);
        Map map = new HashMap();
        map.put("dataList",itCrmQuestionGroupItemAreaDao.list(sql,params.toArray()));
        return map;
    }
}
